/*
** str2tab.c for ftp in /home/morgan_t/Documents/svn-for-my-proj/tek2/UNIX/my_ftp/src/my
** 
** Made by thomas morgant
** Login   <morgan_t@epitech.net>
** 
** Started on  Sun Apr  8 16:16:06 2012 thomas morgant
** Last update Sun Apr  8 16:16:07 2012 thomas morgant
*/

#include "ftp.h"

void            free_tab(char **tab)
{
  int           i;

  i = 0;
  while (tab[i] != NULL)
    {
      free(tab[i]);
      i++;
    }
  free(tab);
}

int             my_tablen(char **tab)
{
  int           i;

  i = 0;
  while (tab[i] != NULL)
    i++;
  return (i);
}

int             count_word(char *str, char sep)
{
  int           i;
  int           nb_word;

  i = 0;
  nb_word = 1;
  while (str[i] != '\0')
    {
      if (str[i] == sep && (i > 0 && str[i - 1] != sep))
        {
          while (str[i] != '\0' && str[i] == sep)
            i++;
          if (str[i] != '\0')
            nb_word++;
        }
      else
        i++;
    }
  return (nb_word);
}

char            *get_word(char *str, int tmp, int len)
{
  int           i;
  char          *res;

  i = 0;
  res = malloc((len + 1) * sizeof(*res));
  while (i < len)
    res[i++] = str[tmp++];
  res[i] = '\0';
  return (res);
}

char            **str_to_wordtab(char *str, char sep)
{
  char          **res;
  int           lines;
  int           len;
  int           tmp;
  int           i;

  res = malloc((count_word(str, sep) + 1) * sizeof(*res));
  lines = 0;
  i = 0;
  while (str[i] != '\0')
    {
      if (str[i] != sep)
        {
          tmp = i;
          len = 0;
          while (str[i] != '\0' && str[i++] != sep)
            len++;
          res[lines] = get_word(str, tmp, len);
          lines++;
        }
      else
        i++;
    }
  res[lines] = NULL;
  return (res);
}
